package com.hnkywl.sts.action.cggl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cggl.CgdxDao;
import com.hnkywl.sts.dao.djgl.CgdmxxDao;
import com.hnkywl.sts.dao.djgl.GlfyxDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.cggl.search.QueryCgd;
import com.hnkywl.sts.entity.djgl.Cgdx;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.cggl.CgdService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.util.EasyuiAdapter;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */
@Namespaces({@Namespace("/cggl/cgdgl"), @Namespace("/cggl/cgdtz")})
public class CgdAction extends SimpleActionSupport {
    //发货开始时间
    private Date fhrqStart;
    //发货结束时间
    private Date fhrqEnd;
    //采购结算单ID
    private Long cgjsdId;
    //采购单号
    private String cgdh;
    private String htbm;
    //页面当前位置
    private int page;
    //每页行数
    private int rows;
    //行索引
    private int index;
    private Long khId;

    private Long sbgsId;

    private Long cggsId;

    private Long cgbmId;

    private Long cgrId;

    private Long gcId;
    private Long cghtId;

    private Long gysid;

    private String zt;

    private String rkzt;

    private String jszt;

    private Date sysdate;

    private Date datetime;

    private String ewmtime;

    private Cgd cgd;

    private GsjgService gsjgService;
    //查询
    private QueryCgd queryCgd;

    private CgdService cgdService;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private Long cgdId;

    private Double ce;

    private String cglx;

    private Long cgdwId;

    private Long dzckId;

    private String sftzd;


    private String gysJc;
    private String zdr;
    private Date CurrentTime = new Date();
    private String htType;

    private String wzbm;

    private Long ggId;

    private Long cdId;
    private Boolean dlkhcgd = true;
    private CgdxDao cgdxDao;
    private CgdmxxDao cgdmxxDao;
    private GlfyxDao glfyxDao;
    private Date startDate;


    @Action("cgdgl")
    public String cgdgl() {
        startDate = sDaysAfterNowDate(7);
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    private Date sDaysAfterNowDate(int n) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DAY_OF_MONTH, -n);
        return rightNow.getTime();
    }

    @Action("cgdtz")
    public String cgdtz() {
        CurrentTime = new Date();
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }


    @Action("cgdlist")
    public void cgdList() throws ParseException {
        if (cgdwId != null) {
            Gsjg g = this.gsjgService.findGsjgDetail(cgdwId);
            if (g instanceof Nbgs) {
                cggsId = cgdwId;
            } else {
                cgbmId = cgdwId;
            }
            this.renderJson(this.cgdService.findAllCgdList(wzbm, cgdh, htbm, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, zdr, dlkhcgd, page, rows));
        } else {
            this.renderJson(this.cgdService.findAllCgdList(wzbm, cgdh, htbm, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, zdr, dlkhcgd, page, rows));
        }
    }

    @Action("cgd_add")
    public void cgdAdd() {
        try {
            this.cgdService.add(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_stock")
    public void cgdStock() {
        try {
            this.cgdService.stock(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"入库成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_del")
    public void cgdDel() {
        try {
            this.cgdService.delete(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_confirm")
    public void cgdConfirm() {
        try {
            this.cgdService.confirm(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"提交采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_cancel")
    public void cgdCancel() {
        try {
            if (cgd.getSfys() || cgd.getYrk()) {
                renderText("{\"statusCode\":\"300\", \"message\":\"采购单已入库无法取消提交\"}");
            } else {
                this.cgdService.cancel(cgd);
                renderText("{\"statusCode\":\"200\", \"message\":\"取消提交采购单成功\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 采购单列表
     */
//    @Action("cgdlist")
//    public void cgdlist() {
//        String[] propertyNames = {"id", "cgdh", "fhrq", "cglx", "gys.qc", "cggs.qc", "cgbm.qc", "cgr.xm", "zt"};
//        Page<Cgd> cgds = cgdService.findByQuery(Pager.initPager(page, rows), queryCgd);
//        this.renderJson(EasyuiAdapter.getJsonForDataGrid(cgds, propertyNames));
//    }
    @Action("cgdtzlist")
    public void cgdtzList() throws ParseException {
        if (cgdwId != null) {
            Gsjg g = this.gsjgService.findGsjgDetail(cgdwId);
            if (g instanceof Nbgs) {
                cggsId = cgdwId;
            } else {
                cgbmId = cgdwId;
            }
            this.renderJson(this.cgdService.findAllCgdtzList(wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, page, rows));
        } else {
            this.renderJson(this.cgdService.findAllCgdtzList(wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, page, rows));
        }
    }

    @Action("edit_cgdtz")
    public void editCgdtz() {
        try {

            Cgd ycgd = this.cgdService.findCgdDetail(cgd.getId());
            if (ycgd != null && ycgd.isSftzd()) {
                throw new RuntimeException("此采购单已冲销，无需再次冲销!");
            }

            List<Cgdmx> cgdmxList = cgd.getCgdmxList();
            if (!(ycgd.getCglx().equalsIgnoreCase("zddh")) || (ycgd.getCght() != null && "保兑仓".equals(ycgd.getCght().getRzlx().getMc()))) {
                Boolean flag = false;
                labelA:
                for (Cgdmx cgdmx : cgdmxList) {
                    Kh kh = cgdmx.getKh();
                    if (kh != null) {
                        List<Xsmx> xsmxList = kh.getXsmxList();
                        if (xsmxList != null) {
                            for (Xsmx xsmx : xsmxList) {
                                Xsd xsd = xsmx.getXsd();
                                if (xsd != null && (xsd.getSftzd() == null || !xsd.getSftzd())) {
                                    flag = true;
                                    break labelA;
                                }
                            }
                        }
                    }
                }
                if (flag)
                    renderText("{\"statusCode\":\"300\", \"message\":\"调整的采购单有对应的销售单，请先冲销销售单\"}");
                else {
                    ReflectionUtils.getInstance(cgd, ycgd);
                    initCgd(cgd);
                    Cgd hccgd = new Cgd();
                    ReflectionUtils.getInstance(hccgd, ycgd);
                    initCgd(hccgd);
                    this.cgdService.editCgdtz(cgd, cgdmxList, hccgd, ycgd);
                    renderText("{\"statusCode\":\"200\", \"message\":\"调整采购单成功\"}");
                }
            } else {
                ReflectionUtils.getInstance(cgd, ycgd);
                initCgd(cgd);
                Cgd hccgd = new Cgd();
                ReflectionUtils.getInstance(hccgd, ycgd);
                initCgd(hccgd);
                this.cgdService.editCgdtz(cgd, cgdmxList, hccgd, ycgd);
                renderText("{\"statusCode\":\"200\", \"message\":\"调整采购单成功\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //生成补充采购单
    @Action("bccgd")
    public void createBcCgd() {
        try {
            Cgd ycgd = this.cgdService.findCgdDetail(cgd.getId());
            if (!"bght".equals(ycgd.getCglx())) {
                renderText("{\"statusCode\":\"300\", \"message\":\"无法生成采购单，必须为保供类型!\"}");
                return;
            }

            if (!ycgd.getYrk()) {
                renderText("{\"statusCode\":\"300\", \"message\":\"无法生成采购单，必须已入库！\"}");
                return;
            }

            if (ycgd.getBccgd() != null) {
                renderText("{\"statusCode\":\"300\", \"message\":\"无法生成采购单，已生成补充采购单！\"}");
                return;
            }

            this.cgdService.saveBccgd(ycgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"生成保供补充采购单成功，采购单号为" + ycgd.getBccgd().getCgdh() + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    private void initCgd(Cgd cgd) {
        if (cgd.getLj() != null && cgd.getLj().getId() == null) {
            cgd.setLj(null);
        }
        if (cgd.getSbgs() != null && cgd.getSbgs().getId() == null) {
            cgd.setSbgs(null);
        }
        if (cgd.getSbbm() != null && cgd.getSbbm().getId() == null) {
            cgd.setSbbm(null);
        }

        if (cgd.getSbr() != null && cgd.getSbr().getId() == null) {
            cgd.setSbr(null);
        }
        if (cgd.getFhgs() != null && cgd.getFhgs().getId() == null) {
            cgd.setFhgs(null);
        }
        if (cgd.getYsdw() != null && cgd.getYsdw().getId() == null) {
            cgd.setYsdw(null);
        }
        if (cgd.getCght() != null && cgd.getCght().getId() == null) {
            cgd.setCght(null);
        }
        if (cgd.getDzck() != null && cgd.getDzck().getId() == null) {
            cgd.setDzck(null);
        }
        if (cgd.getCggs() != null && cgd.getCggs().getId() == null) {
            cgd.setCggs(null);
        }
        if (cgd.getCgbm() != null && cgd.getCgbm().getId() == null) {
            cgd.setCgbm(null);
        }
        if (cgd.getCgr() != null && cgd.getCgr().getId() == null) {
            cgd.setCgr(null);
        }

        if (cgd.getXsd() != null && cgd.getXsd().getId() == null) {
            cgd.setXsd(null);
        }

        if (cgd.getCgdmxList() != null) {
            for (Cgdmx cgdmx : cgd.getCgdmxList()) {
                if (cgdmx.getJzfs() != null && cgdmx.getJzfs().getId() == null) {
                    cgdmx.setJzfs(null);
                }
                if (cgdmx.getCd() != null && cgdmx.getCd().getId() == null) {
                    cgdmx.setCd(null);
                }
                if (cgdmx.getKh() != null && cgdmx.getKh().getId() == null) {
                    cgdmx.setKh(null);
                }
            }
        }
    }

    /**
     * 采购记录记录
     */
    @Action("cgjlList")
    public void cgjlList() {
        String[] propertyNames = {"id", "cgdh", "fhrq", "cglx", "gys.qc", "sbgs.qc", "sbbm.qc", "sbr.xm", "zt"};
        Page<Cgd> cgds = cgdService.findCgjlQuery(cghtId, page, rows);
        this.renderJson(EasyuiAdapter.getJsonForDataGrid(cgds, propertyNames));
    }


    @Action("cgdtzApply")
    public void cgdtzApply() {
        try {
            this.cgdService.cgdtzApply(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"申请采购单调整成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgdtzCancel")
    public void cgdtzCancel() {
        try {
            this.cgdService.cgdtzCancel(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"取消申请采购单调整成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgdtzExamine")
    public void cgdtzExamine() {
        try {
            this.cgdService.cgdtzExamine(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"审批采购单调整成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 采购单
     */
    @Actions({@Action("cgdgl_detail"), @Action("cgdtz_detail")})
    public String cgddetail() {
        Cgdx cgdx = this.cgdxDao.get(cgd.getId());
        if (cgdx != null) {
            ReflectionUtils.getInstance(cgd, cgdx);
            cgdxDao.setCgdBaseFields(cgd, cgdx);
        } else {
            cgd = this.cgdService.findById(cgd.getId());
        }
        jszt = this.cgdService.findCgdJszt(cgd);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        if (cgd != null) {
            ewmtime = simpleDateFormat.format(new Date());
        } else {
            ewmtime = "";
        }

        return SUCCESS;
    }

    @Actions({@Action("cgdgl_content"), @Action("cgdtz_content")})
    public String cgdcontent() {
        if (cgd != null && cgd.getId() != null) {
            cgd = this.cgdService.findById(cgd.getId());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            ewmtime = simpleDateFormat.format(cgd.getLastUpdated());
        } else {
            cgd = new Cgd();
            sysdate = new Date(); //当前日期
            Calendar calendar = Calendar.getInstance();//日历对象
            calendar.setTime(sysdate);//设置当前日期
            calendar.add(Calendar.MONTH, 1);//将当前日期加一个月
            datetime = calendar.getTime();//输出格式化的日期
        }
        return SUCCESS;
    }

    @Action("cgdgl_stock")
    public String cgdstock() {
        cgd = this.cgdService.findById(cgd.getId());
        return SUCCESS;
    }

    @Action("cgdgl_cgmx")
    public void cgdglCgmx() {
        Gson gson = new Gson();
        if (cgd.getId() != null) {
            List<Map> cgmxs;
            Cgdx cgdx = cgdxDao.get(cgd.getId());
            if (cgdx != null) {
                cgmxs = this.cgdmxxDao.findCgmxx(cgd.getId());
            } else {
                cgmxs = this.cgdService.findCgmx(cgd.getId());
            }
            Integer js = 0;
            Double sbsl = 0d;
            Double hsj = 0d;
            Double bhsj = 0d;
            Integer rkjs = 0;
            Double rksl = 0d;
            Double rkjz = 0d;
            String result;
            if (cgmxs.size() > 0) {
                for (Map map : cgmxs) {
                    js += map.get("js") == null ? 0 : (Integer) map.get("js");
                    sbsl = sbsl + (map.get("sbsl") == null ? 0d : Double.parseDouble(map.get("sbsl").toString()));
                    hsj = hsj + (map.get("hsj") == null ? 0d : Double.parseDouble(map.get("hsj").toString()));
                    bhsj = bhsj + (map.get("bhsj") == null ? 0d : Double.parseDouble(map.get("bhsj").toString()));
                    rkjs = rkjs + (map.get("rkjs") == null ? 0 : Integer.parseInt(map.get("rkjs").toString()));
                    rksl = rksl + (map.get("rksl") == null ? 0d : Double.parseDouble(map.get("rksl").toString()));
                    rkjz = rkjz + (map.get("rkjz") == null ? 0d : Double.parseDouble(map.get("rkjz").toString()));
                }
                result = "{\"rows\":" + gson.toJson(cgmxs) + ",\"footer\":[{\"cd.id\":\"合计:\",\"js\":" + js
                        + ",\"sbsl\":" + sbsl + ",\"hsj\":" + hsj + ",\"bhsj\":" + bhsj + ",\"rkjs\":" + rkjs + ",\"rksl\":" + rksl + ",\"rkjz\":" + rkjz + "}]}";
            } else {
                result = "{\"rows\":" + gson.toJson(cgmxs) + "}]}";
            }
            this.renderJson(result);
        } else {
            this.renderJson("");
        }
    }

    /**
     * 采购结算调用查询采购单明细
     */
    @Action("cgjsdSearch_cgdmx")
    public void cgjsdSreachCgmx() {
        Gson gson = new Gson();
        List<Map> cgmxs = this.cgdService.findCgjsdSearchCgmx(cgd.getId());
        Integer js = 0;
        Double sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Integer rkjs = 0;
        Double rksl = 0d;
        Double rkjz = 0d;
        String result;
        if (cgmxs.size() > 0) {
            for (Map map : cgmxs) {
                js += map.get("js") == null ? 0 : (Integer) map.get("js");
                sbsl = sbsl + (map.get("sbsl") == null ? 0d : Double.parseDouble(map.get("sbsl").toString()));
                hsj = hsj + (map.get("hsj") == null ? 0d : Double.parseDouble(map.get("hsj").toString()));
                bhsj = bhsj + (map.get("bhsj") == null ? 0d : Double.parseDouble(map.get("bhsj").toString()));
                rkjs = rkjs + (map.get("rkjs") == null ? 0 : Integer.parseInt(map.get("rkjs").toString()));
                rksl = rksl + (map.get("rksl") == null ? 0d : Double.parseDouble(map.get("rksl").toString()));
                rkjz = rkjz + (map.get("rkjz") == null ? 0d : Double.parseDouble(map.get("rkjz").toString()));
            }
            result = "{\"rows\":" + gson.toJson(cgmxs) + ",\"footer\":[{\"cd.id\":\"合计:\",\"js\":" + js
                    + ",\"sbsl\":" + sbsl + ",\"hsj\":" + hsj + ",\"bhsj\":" + bhsj + ",\"rkjs\":" + rkjs + ",\"rksl\":" + rksl + ",\"rkjz\":" + rkjz + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(cgmxs) + "}]}";
        }
        this.renderJson(result);
    }


    @Action("cgdgl_cgfy")
    public void cgdglCgfy() {
        if (cgd.getId() != null) {
            Gson gson = new Gson();
            String result;
            Double jfsl = 0d;
            Double je = 0d;
            List<Map> cgfys;
            Cgdx cgdx = cgdxDao.get(cgd.getId());
            if (cgdx != null) {
                cgfys = this.glfyxDao.findCgfyx(cgd.getId());
            } else {
                cgfys = this.cgdService.findCgfy(cgd.getId());
            }
            if (cgfys.size() > 0) {
                for (Map map : cgfys) {
                    jfsl += map.get("jfsl") == null ? 0d : (Double) map.get("jfsl");
                    je += map.get("je") == null ? 0d : (Double) map.get("je");
                }
                result = "{\"rows\":" + gson.toJson(this.cgdService.findCgfy(cgd.getId())) + ",\"footer\":[{\"fylb.fykmjc\":\"合计:\",\"jfsl\":" + jfsl
                        + ",\"je\":" + je + "}]}";
            } else {
                result = "{\"rows\":" + gson.toJson(this.cgdService.findCgfy(cgd.getId())) + "}";
            }
            this.renderJson(result);
        } else {
            this.renderJson("");
        }
    }

    @Action("cgdmxByCgdId")
    public void cgdmxByCgdId() {
        this.renderJson(this.cgdService.findCgdmxByCgdId(cgdId, ce));
    }

    //采购结算单中查询采购单
    @Action("cgdSearchList")
    public void cgdSearchList() {
        this.renderJson(this.cgdService.findCgdSearch(cgdh, fhrqStart, fhrqEnd, cgjsdId, cggsId, gysid, this.page, this.rows));
    }

    //采购结算单中查询采购单
    @Action("cgddmxSearchList")
    public void cgddmxSearchList() {
        this.renderJson(this.cgdService.findCgddmxSearch(cgjsdId));
    }

    //采购结算单中查询采购单
    @Action("cgdmxSearchList")
    public void cgdmxSearchList() {
        this.renderJson(this.cgdService.findCgdmxSearch(cgjsdId));
    }

    @Action("cgdGysList")
    public void cgdGysList() {
        this.renderJson(this.cgdService.findGysByCgd(cggsId, sbgsId, gysJc));
    }

    //采购单打印
    @Action("cgdDetail")
    public void cgdDetail() {
        List<Map> cgdmxList;
        Cgdx cgdx = cgdxDao.get(cgdId);
        if (cgdx != null) {
            cgdmxList = this.cgdmxxDao.findCgmxx(cgdId);
        } else {
            cgdmxList = this.cgdService.findCgmx(cgdId);
        }
        Double sbsl = 0d;
        Double rksl = 0d;
        Double yshje = 0d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        StringBuffer sbTable = new StringBuffer();
        if (null != cgdmxList) {
            sbTable.append("<table class=\"tb intb\"><thead>");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:100px;' name = 'newCode'>新物资编码</td><td style='width:100px;display:none' name = 'oldCode'>老物资编码</td><td width='30px'>品名</td><td>规格型号</td><td width='60px'>图号/材质</td>" +
                    "<td width='30px'>单位</td><td width='60px'>应送货数量</td><td width='60px'>已送货数量</td><td>单价(含税)</td><td>总金额(含税)</td>");
            sbTable.append("</tr></thead><tbody>");

            for (int i = 0; i < cgdmxList.size(); i++) {
                sbTable.append("<tr>");
                sbTable.append("<td name = 'newCode' >");
                if (null != cgdmxList.get(i).get("wzgg.code")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.code"));
                }
                sbTable.append("</td>");

                sbTable.append("<td name = 'oldCode' style='display:none'>");
                if (null != cgdmxList.get(i).get("wzgg.oldcode")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.oldcode"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.name")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

//                sbTable.append("<td>");
//                if (null != cgdmxList.get(i).get("th")) {
//                    sbTable.append(cgdmxList.get(i).get("th"));
//                }
//                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.cz")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.cz"));
                }
                sbTable.append("</td>");
//                sbTable.append("<td>");
//                if (null != cgdmxList.get(i).get("ph")) {
//                    sbTable.append(cgdmxList.get(i).get("ph"));
//                }
//                sbTable.append("</td>");

//                sbTable.append("<td>");
//                if (null != cgdmxList.get(i).get("ck")) {
//                    sbTable.append(cgdmxList.get(i).get("ck"));
//                }
//                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.dw")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("sbsl")) {
                    Double msbsl = (Double) cgdmxList.get(i).get("sbsl");
                    BigDecimal bsbsl = new BigDecimal(msbsl);
                    Double dsbsl = bsbsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dsbsl));
                    sbsl += dsbsl;
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("rksl")) {
                    Double mrksl = (Double) cgdmxList.get(i).get("rksl");
                    BigDecimal brksl = new BigDecimal(mrksl);
                    Double drksl = brksl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(drksl));
                    rksl += drksl;
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("hsj")) {
                    sbTable.append(df.format(cgdmxList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("hsj") && null != cgdmxList.get(i).get("rksl")) {
                    Double myshje = ((Double) cgdmxList.get(i).get("hsj")) * ((Double) cgdmxList.get(i).get("rksl"));
                    BigDecimal byshje = new BigDecimal(myshje);
                    Double dyshje = byshje.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(jtdf.format(dyshje));
                    yshje += dyshje;
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"5\" name = 'colspanCount'>合计");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(rksl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(jtdf.format(yshje));
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</tbody></table>");
        }
        renderHtml(sbTable.toString());
    }

    //保供采购单打印
    @Action("bgcgdDetail")
    public void bgcgdDetail() {
        List<Map> cgdmxList = this.cgdService.findCgmx(cgdId);
        Double sbsl = 0d;
        Double rksl = 0d;
        Double yshje = 0d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        StringBuffer sbTable = new StringBuffer();
        if (null != cgdmxList) {
            sbTable.append("<table class=\"tb intb\"><thead>");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:100px;'>物资编码</td><td width='30px'>品名</td><td>规格型号</td><td width='60px'>图号/材质</td><td width='60px'>牌号商标</td>" +
                    "<td width='30px'>单位</td><td width='60px'>应送货数量</td><td width='60px'>已送货数量</td><td>单价(含税)</td><td>金额(含税)</td>");
            sbTable.append("</tr></thead><tbody>");

            for (int i = 0; i < cgdmxList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.oldcode")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.oldcode"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.name")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("th")) {
                    sbTable.append(cgdmxList.get(i).get("th"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("ph")) {
                    sbTable.append(cgdmxList.get(i).get("ph"));
                }
                sbTable.append("</td>");


                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.dw")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("sbsl")) {
                    Double msbsl = (Double) cgdmxList.get(i).get("sbsl");
                    BigDecimal bsbsl = new BigDecimal(msbsl);
                    Double dsbsl = bsbsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dsbsl));
                    sbsl += dsbsl;
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("rksl")) {
                    Double mrksl = (Double) cgdmxList.get(i).get("rksl");
                    BigDecimal brksl = new BigDecimal(mrksl);
                    Double drksl = brksl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(drksl));
                    rksl += drksl;
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("hsj")) {
                    sbTable.append(df.format(cgdmxList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("hsj") && null != cgdmxList.get(i).get("rksl")) {
                    Double myshje = ((Double) cgdmxList.get(i).get("hsj")) * ((Double) cgdmxList.get(i).get("rksl"));
                    BigDecimal byshje = new BigDecimal(myshje);
                    Double dyshje = byshje.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(jtdf.format(dyshje));
                    yshje += dyshje;
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"6\">合计");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(rksl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(jtdf.format(yshje));
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</tbody></table>");
        }
        renderHtml(sbTable.toString());
    }

    //获取件重
    @Action("get_jz")
    public void findJz() {
        this.renderText(this.cgdService.findJz(ggId, cdId));
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getCgdId() {
        return cgdId;
    }

    public void setCgdId(Long cgdId) {
        this.cgdId = cgdId;
    }

    public Double getCe() {
        return ce;
    }

    public void setCe(Double ce) {
        this.ce = ce;
    }

    public int getIndex() {
        return index;
    }

    public String getZdr() {
        return zdr;
    }

    public void setZdr(String zdr) {
        this.zdr = zdr;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Autowired
    public void setCgdService(CgdService cgdService) {
        this.cgdService = cgdService;
    }

    public QueryCgd getQueryCgd() {
        return queryCgd;
    }

    public void setQueryCgd(QueryCgd queryCgd) {
        this.queryCgd = queryCgd;
    }

    public Date getSysdate() {
        return sysdate;
    }

    public void setSysdate(Date sysdate) {
        this.sysdate = sysdate;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public Date getFhrqStart() {
        return fhrqStart;
    }

    public void setFhrqStart(Date fhrqStart) {
        this.fhrqStart = fhrqStart;
    }

    public Date getFhrqEnd() {
        return fhrqEnd;
    }

    public void setFhrqEnd(Date fhrqEnd) {
        this.fhrqEnd = fhrqEnd;
    }

    public Long getCgjsdId() {
        return cgjsdId;
    }

    public void setCgjsdId(Long cgjsdId) {
        this.cgjsdId = cgjsdId;
    }

    public String getCgdh() {
        return cgdh;
    }

    public void setCgdh(String cgdh) {
        this.cgdh = cgdh;
    }

    public String getHtbm() {
        return htbm;
    }

    public void setHtbm(String htbm) {
        this.htbm = htbm;
    }

    public Long getKhId() {
        return khId;
    }

    public Long getCgdwId() {
        return cgdwId;
    }

    public void setCgdwId(Long cgdwId) {
        this.cgdwId = cgdwId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public Long getCggsId() {
        return cggsId;
    }

    public void setCggsId(Long cggsId) {
        this.cggsId = cggsId;
    }

    public Long getCgbmId() {
        return cgbmId;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public void setCgbmId(Long cgbmId) {
        this.cgbmId = cgbmId;
    }

    public Long getCgrId() {
        return cgrId;
    }

    public void setCgrId(Long cgrId) {
        this.cgrId = cgrId;
    }

    public Long getGcId() {
        return gcId;
    }

    public void setGcId(Long gcId) {
        this.gcId = gcId;
    }

    public Long getCghtId() {
        return cghtId;
    }

    public void setCghtId(Long cghtId) {
        this.cghtId = cghtId;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getJszt() {
        return jszt;
    }

    public void setJszt(String jszt) {
        this.jszt = jszt;
    }

    public Cgd getCgd() {
        return cgd;
    }

    public void setCgd(Cgd cgd) {
        this.cgd = cgd;
    }

    public Long getGysid() {
        return gysid;
    }


    public void setGysid(Long gysid) {
        this.gysid = gysid;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getCglx() {
        return cglx;
    }

    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public void setCglx(String cglx) {
        this.cglx = cglx;
    }

    public String getEwmtime() {
        return ewmtime;
    }

    public void setEwmtime(String ewmtime) {
        this.ewmtime = ewmtime;
    }

    public Long getDzckId() {
        return dzckId;
    }

    public void setDzckId(Long dzckId) {
        this.dzckId = dzckId;
    }

    public Long getGgId() {
        return ggId;
    }

    public void setGgId(Long ggId) {
        this.ggId = ggId;
    }

    public Long getCdId() {
        return cdId;
    }

    public void setCdId(Long cdId) {
        this.cdId = cdId;
    }

    public String getSftzd() {
        return sftzd;
    }

    public void setSftzd(String sftzd) {
        this.sftzd = sftzd;
    }

    public String getHtType() {
        return htType;
    }

    public void setHtType(String htType) {
        this.htType = htType;
    }

    public String getRkzt() {
        return rkzt;
    }

    public void setRkzt(String rkzt) {
        this.rkzt = rkzt;
    }

    public String getGysJc() {
        return gysJc;
    }

    public void setGysJc(String gysJc) {
        this.gysJc = gysJc;
    }

    public Boolean getDlkhcgd() {
        return dlkhcgd;
    }

    public void setDlkhcgd(Boolean dlkhcgd) {
        this.dlkhcgd = dlkhcgd;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setKhDao(KhDao khDao) {
    }

    @Autowired
    public void setCgdxDao(CgdxDao cgdxDao) {
        this.cgdxDao = cgdxDao;
    }

    @Autowired
    public void setCgdmxxDao(CgdmxxDao cgdmxxDao) {
        this.cgdmxxDao = cgdmxxDao;
    }

    @Autowired
    public void setGlfyxDao(GlfyxDao glfyxDao) {
        this.glfyxDao = glfyxDao;
    }
}
